//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "AmortizingCmsRateBond.h"
using namespace Cephei::QL::Experimental::Amortizingbonds;
#include <gen/QL/Times/Schedule.h>
#include <gen/QL/Indexes/SwapIndex.h>
#include <gen/QL/Times/DayCounter.h>
#include <gen/QL/Times/Calendar.h>
#include <gen/QL/CashFlow.h>
#include <gen/QL/PricingEngine.h>
#include <gen/QL/Instruments/Bond.h>
using namespace Cephei::QL::Times;
using namespace Cephei::QL::Indexes;
using namespace Cephei::QL;
using namespace Cephei::QL::Instruments;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Experimental::Amortizingbonds::CAmortizingCmsRateBond::CAmortizingCmsRateBond (UInt32 settlementDays, Cephei::Core::IVector<Double>^ notionals, Cephei::QL::Times::ISchedule^ schedule, Cephei::QL::Indexes::ISwapIndex^ index, Cephei::QL::Times::IDayCounter^ paymentDayCounter, Microsoft::FSharp::Core::FSharpOption<QL::Times::BusinessDayConventionEnum>^ paymentConvention, Microsoft::FSharp::Core::FSharpOption<UInt32>^ fixingDays, Microsoft::FSharp::Core::FSharpOption<Cephei::Core::IVector<Double>^>^ gearings, Microsoft::FSharp::Core::FSharpOption<Cephei::Core::IVector<Double>^>^ spreads, Microsoft::FSharp::Core::FSharpOption<Cephei::Core::IVector<Double>^>^ caps, Microsoft::FSharp::Core::FSharpOption<Cephei::Core::IVector<Double>^>^ floors, Microsoft::FSharp::Core::FSharpOption<Boolean>^ inArrears, Microsoft::FSharp::Core::FSharpOption<DateTime>^ issueDate, Cephei::QL::IPricingEngine^ QL_Pricer) : CBond(CAmortizingCmsRateBond::typeid)
{
    CSchedule^ _Cschedule;
    CSwapIndex^ _Cindex;
    CDayCounter^ _CpaymentDayCounter;
    try
    {
#ifdef HANDLE
        _phAmortizingCmsRateBond = NULL;
#endif
        QuantLib::Natural _settlementDays = (QuantLib::Natural)ValueHelper::Convert (settlementDays); //d
        notionals->Lock();
        INativeVector<Double>^ _NCInotionals = notionals->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCnotionals = safe_cast<CDoubleVector^>(_NCInotionals);
        std::vector<QuantLib::Real>& _notionals = static_cast<std::vector<QuantLib::Real>&> (_NCnotionals->GetReference ());
        _Cschedule = safe_cast<CSchedule^> (schedule);
        _Cschedule->Lock();
        QuantLib::Schedule& _schedule = static_cast<QuantLib::Schedule&> (_Cschedule->GetReference ()); 
        _Cindex = safe_cast<CSwapIndex^> (index);
        _Cindex->Lock();
        boost::shared_ptr<QuantLib::SwapIndex>& _index = static_cast<boost::shared_ptr<QuantLib::SwapIndex>&> (_Cindex->GetShared ()); 
        _CpaymentDayCounter = safe_cast<CDayCounter^> (paymentDayCounter);
        _CpaymentDayCounter->Lock();
        QuantLib::DayCounter& _paymentDayCounter = static_cast<QuantLib::DayCounter&> (_CpaymentDayCounter->GetReference ()); 
        QuantLib::BusinessDayConvention _paymentConvention = 
            (Microsoft::FSharp::Core::FSharpOption<QL::Times::BusinessDayConventionEnum>::IsSome::get (paymentConvention) ? (QuantLib::BusinessDayConvention)paymentConvention->Value : QuantLib::BusinessDayConvention::Following); //10
        QuantLib::Natural _fixingDays = 
            (Microsoft::FSharp::Core::FSharpOption<UInt32>::IsSome::get (fixingDays) ? (QuantLib::Natural)ValueHelper::Convert (fixingDays->Value) : Null<QuantLib::Natural>()); //4
        CDoubleVector^ _NCgearings;
        if (Microsoft::FSharp::Core::FSharpOption<Cephei::Core::IVector<Double>^>::IsSome::get (gearings))
        {
            gearings->Value->Lock();
            INativeVector<Double>^ _NCIgearings = gearings->Value->getFeature (NativeFeature::Value);
            _NCgearings = safe_cast<CDoubleVector^>(_NCIgearings);
        }
        std::vector<QuantLib::Real>& _gearings = 
            (Microsoft::FSharp::Core::FSharpOption<Cephei::Core::IVector<Double>^>::IsSome::get (gearings) ? static_cast<std::vector<QuantLib::Real>&> (_NCgearings->GetReference ()) : std::vector<QuantLib::Real>(1, 1.0)); //3
        CDoubleVector^ _NCspreads;
        if (Microsoft::FSharp::Core::FSharpOption<Cephei::Core::IVector<Double>^>::IsSome::get (spreads))
        {
            spreads->Value->Lock();
            INativeVector<Double>^ _NCIspreads = spreads->Value->getFeature (NativeFeature::Value);
            _NCspreads = safe_cast<CDoubleVector^>(_NCIspreads);
        }
        std::vector<QuantLib::Spread>& _spreads = 
            (Microsoft::FSharp::Core::FSharpOption<Cephei::Core::IVector<Double>^>::IsSome::get (spreads) ? static_cast<std::vector<QuantLib::Spread>&> (_NCspreads->GetReference ()) : std::vector<QuantLib::Spread>(1, 0.0)); //3
        CDoubleVector^ _NCcaps;
        if (Microsoft::FSharp::Core::FSharpOption<Cephei::Core::IVector<Double>^>::IsSome::get (caps))
        {
            caps->Value->Lock();
            INativeVector<Double>^ _NCIcaps = caps->Value->getFeature (NativeFeature::Value);
            _NCcaps = safe_cast<CDoubleVector^>(_NCIcaps);
        }
        std::vector<QuantLib::Rate>& _caps = 
            (Microsoft::FSharp::Core::FSharpOption<Cephei::Core::IVector<Double>^>::IsSome::get (caps) ? static_cast<std::vector<QuantLib::Rate>&> (_NCcaps->GetReference ()) : std::vector<QuantLib::Rate>()); //3
        CDoubleVector^ _NCfloors;
        if (Microsoft::FSharp::Core::FSharpOption<Cephei::Core::IVector<Double>^>::IsSome::get (floors))
        {
            floors->Value->Lock();
            INativeVector<Double>^ _NCIfloors = floors->Value->getFeature (NativeFeature::Value);
            _NCfloors = safe_cast<CDoubleVector^>(_NCIfloors);
        }
        std::vector<QuantLib::Rate>& _floors = 
            (Microsoft::FSharp::Core::FSharpOption<Cephei::Core::IVector<Double>^>::IsSome::get (floors) ? static_cast<std::vector<QuantLib::Rate>&> (_NCfloors->GetReference ()) : std::vector<QuantLib::Rate>()); //3
        bool _inArrears = 
            (Microsoft::FSharp::Core::FSharpOption<Boolean>::IsSome::get (inArrears) ? (bool)ValueHelper::Convert (inArrears->Value) : false); //4
        QuantLib::Date _issueDate = 
            (Microsoft::FSharp::Core::FSharpOption<DateTime>::IsSome::get (issueDate) ? (QuantLib::Date)ValueHelper::Convert (issueDate->Value) : QuantLib::Date()); //4
        _ppAmortizingCmsRateBond = new boost::shared_ptr<QuantLib::AmortizingCmsRateBond> (new QuantLib::AmortizingCmsRateBond ( _settlementDays,  _notionals,  _schedule,  _index,  _paymentDayCounter,  _paymentConvention,  _fixingDays,  _gearings,  _spreads,  _caps,  _floors,  _inArrears,  _issueDate ));
        CPricingEngine^ _CQL_Pricer = safe_cast<CPricingEngine^> (QL_Pricer);
        boost::shared_ptr<QuantLib::PricingEngine>& _QL_Pricer = static_cast<boost::shared_ptr<QuantLib::PricingEngine>&> (_CQL_Pricer->GetShared ());
        (*_ppAmortizingCmsRateBond)->setPricingEngine (_QL_Pricer);
        SetBond (boost::dynamic_pointer_cast<QuantLib::Bond> (*_ppAmortizingCmsRateBond));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (notionals != nullptr) notionals->Unlock();    //not optional
        if (_Cschedule != nullptr) _Cschedule->Unlock();
        if (_Cindex != nullptr) _Cindex->Unlock();
        if (_CpaymentDayCounter != nullptr) _CpaymentDayCounter->Unlock();
        if (gearings != nullptr) gearings->Value->Unlock();
        if (spreads != nullptr) spreads->Value->Unlock();
        if (caps != nullptr) caps->Value->Unlock();
        if (floors != nullptr) floors->Value->Unlock();
    }
}
Cephei::QL::Experimental::Amortizingbonds::CAmortizingCmsRateBond::CAmortizingCmsRateBond (boost::shared_ptr<QuantLib::AmortizingCmsRateBond>& childNative, Object^ owner) : CBond(CAmortizingCmsRateBond::typeid)
{
#ifdef HANDLE
	_phAmortizingCmsRateBond = NULL;
#endif
	_ppAmortizingCmsRateBond = &childNative;
    _ppBond = new boost::shared_ptr<QuantLib::Bond> (boost::dynamic_pointer_cast<QuantLib::Bond> (*_ppAmortizingCmsRateBond));
}
Cephei::QL::Experimental::Amortizingbonds::CAmortizingCmsRateBond::CAmortizingCmsRateBond (QuantLib::AmortizingCmsRateBond& childNative, Object^ owner) : CBond(CAmortizingCmsRateBond::typeid)
{
#ifdef HANDLE
	_phAmortizingCmsRateBond = NULL;
#endif
	_ppAmortizingCmsRateBond = new boost::shared_ptr<QuantLib::AmortizingCmsRateBond> (&childNative);
    _ppBond = new boost::shared_ptr<QuantLib::Bond> (boost::dynamic_pointer_cast<QuantLib::Bond> (*_ppAmortizingCmsRateBond));
    _AmortizingCmsRateBondOwner = owner;
    _BondOwner = owner;
}

Cephei::QL::Experimental::Amortizingbonds::CAmortizingCmsRateBond::CAmortizingCmsRateBond (CAmortizingCmsRateBond^ copy) : CBond(CAmortizingCmsRateBond::typeid)
{
#ifdef HANDLE
	_phAmortizingCmsRateBond = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppAmortizingCmsRateBond = new boost::shared_ptr<QuantLib::AmortizingCmsRateBond> (copy->GetShared());
        _ppBond = new boost::shared_ptr<QuantLib::Bond> (boost::dynamic_pointer_cast<QuantLib::Bond> (*_ppAmortizingCmsRateBond));
    }
}
Cephei::QL::Experimental::Amortizingbonds::CAmortizingCmsRateBond::CAmortizingCmsRateBond (PLATFORM::Type^ t) : CBond(CAmortizingCmsRateBond::typeid)
{
#ifdef HANDLE
	_phAmortizingCmsRateBond = NULL;
#endif
	if (!t->IsSubclassOf(CAmortizingCmsRateBond::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Experimental::Amortizingbonds::CAmortizingCmsRateBond::CAmortizingCmsRateBond (QuantLib::Handle<QuantLib::AmortizingCmsRateBond>& childNative, Object^ owner)  : CBond(CAmortizingCmsRateBond::typeid)
{
	_phAmortizingCmsRateBond = &childNative;
	_ppAmortizingCmsRateBond = &static_cast<boost::shared_ptr<QuantLib::AmortizingCmsRateBond>>(childNative.currentLink());
    _ppBond = new boost::shared_ptr<QuantLib::Bond> (boost::dynamic_pointer_cast<QuantLib::Bond> (*_ppAmortizingCmsRateBond));
    _AmortizingCmsRateBondOwner = owner;
}
Cephei::QL::Experimental::Amortizingbonds::CAmortizingCmsRateBond::CAmortizingCmsRateBond (QuantLib::Handle<QuantLib::AmortizingCmsRateBond> childNative)  : CBond(CAmortizingCmsRateBond::typeid)
{
	_phAmortizingCmsRateBond = &childNative;
	_ppAmortizingCmsRateBond = &static_cast<boost::shared_ptr<QuantLib::AmortizingCmsRateBond>>(childNative.currentLink());
    _ppBond = new boost::shared_ptr<QuantLib::Bond> (boost::dynamic_pointer_cast<QuantLib::Bond> (*_ppAmortizingCmsRateBond));
}
#endif
#ifdef STRUCT
Cephei::QL::Experimental::Amortizingbonds::CAmortizingCmsRateBond::CAmortizingCmsRateBond (QuantLib::AmortizingCmsRateBond childNative)  : CBond(CAmortizingCmsRateBond::typeid)
{
#ifdef HANDLE
	_phAmortizingCmsRateBond = NULL;
#endif
	_ppAmortizingCmsRateBond = new boost::shared_ptr<QuantLib::AmortizingCmsRateBond> (new QuantLib::AmortizingCmsRateBond (childNative));
    _ppBond = new boost::shared_ptr<QuantLib::Bond> (boost::dynamic_pointer_cast<QuantLib::Bond> (*_ppAmortizingCmsRateBond));
}
#endif

Cephei::QL::Experimental::Amortizingbonds::CAmortizingCmsRateBond::~CAmortizingCmsRateBond ()
{
    if (_ppAmortizingCmsRateBond != NULL)
    {
	    delete _ppAmortizingCmsRateBond;
        _ppAmortizingCmsRateBond = NULL;
    }
}
Cephei::QL::Experimental::Amortizingbonds::CAmortizingCmsRateBond::!CAmortizingCmsRateBond ()
{
    if (_ppAmortizingCmsRateBond != NULL)
    {
	    delete _ppAmortizingCmsRateBond;
    }
}
QuantLib::AmortizingCmsRateBond& Cephei::QL::Experimental::Amortizingbonds::CAmortizingCmsRateBond::GetReference ()
{
    if (_ppAmortizingCmsRateBond == NULL) throw REFNEW NativeNullException ();
	return **_ppAmortizingCmsRateBond;
}
boost::shared_ptr<QuantLib::AmortizingCmsRateBond>& Cephei::QL::Experimental::Amortizingbonds::CAmortizingCmsRateBond::GetShared ()
{
    if (_ppAmortizingCmsRateBond == NULL) throw REFNEW NativeNullException ();
	return *_ppAmortizingCmsRateBond;
}
QuantLib::AmortizingCmsRateBond* Cephei::QL::Experimental::Amortizingbonds::CAmortizingCmsRateBond::GetPointer ()
{
    if (_ppAmortizingCmsRateBond == NULL) throw REFNEW NativeNullException ();
	return &**_ppAmortizingCmsRateBond;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::AmortizingCmsRateBond>& Cephei::QL::Experimental::Amortizingbonds::CAmortizingCmsRateBond::GetHandle ()
{
	if (_phAmortizingCmsRateBond == NULL)
	{
		_phAmortizingCmsRateBond = new Handle<QuantLib::AmortizingCmsRateBond> (*_ppAmortizingCmsRateBond);
	}
	return *_phAmortizingCmsRateBond;
}
#endif
bool Cephei::QL::Experimental::Amortizingbonds::CAmortizingCmsRateBond::HasNative () 
{
	return (_ppAmortizingCmsRateBond != NULL);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Experimental::Amortizingbonds::IAmortizingCmsRateBond^ Cephei::QL::Experimental::Amortizingbonds::CAmortizingCmsRateBond_Factory::Create (UInt32 settlementDays, Cephei::Core::IVector<Double>^ notionals, Cephei::QL::Times::ISchedule^ schedule, Cephei::QL::Indexes::ISwapIndex^ index, Cephei::QL::Times::IDayCounter^ paymentDayCounter, Microsoft::FSharp::Core::FSharpOption<QL::Times::BusinessDayConventionEnum>^ paymentConvention, Microsoft::FSharp::Core::FSharpOption<UInt32>^ fixingDays, Microsoft::FSharp::Core::FSharpOption<Cephei::Core::IVector<Double>^>^ gearings, Microsoft::FSharp::Core::FSharpOption<Cephei::Core::IVector<Double>^>^ spreads, Microsoft::FSharp::Core::FSharpOption<Cephei::Core::IVector<Double>^>^ caps, Microsoft::FSharp::Core::FSharpOption<Cephei::Core::IVector<Double>^>^ floors, Microsoft::FSharp::Core::FSharpOption<Boolean>^ inArrears, Microsoft::FSharp::Core::FSharpOption<DateTime>^ issueDate, Cephei::QL::IPricingEngine^ QL_Pricer)
{
    return REFNEW CAmortizingCmsRateBond ( settlementDays,  notionals,  schedule,  index,  paymentDayCounter,  paymentConvention,  fixingDays,  gearings,  spreads,  caps,  floors,  inArrears,  issueDate,  QL_Pricer);
}
